﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PhoneApplicPersons.Classes;


namespace PhoneApplicPersons
{
    public class DrawPerson : StackPanel
    {
        Persona persona;

        public DrawPerson(Persona per)
        {
            persona = per;
        }

        public void Draw()
        {
            if (persona != null)
            {
                //Image Img_Photo = new Image();
                //Img_Photo.Cursor = Cursors.Hand;
                //Img_Photo.Height = 50;
                //Img_Photo.Width = 50;
                //Img_Photo.VerticalAlignment = VerticalAlignment.Top;
                //Img_Photo.Stretch = Stretch.Fill;
                ////Img_Photo.Source = new Uri("/PhoneApplicPersons;component/Images/photo.png").;
                //this.Children.Add(Img_Photo);
                /// ID
                TextBlock Lbl_ID = new TextBlock();
                Lbl_ID.Text = "ID: " + persona.ID.ToString();
                Lbl_ID.TextDecorations = TextDecorations.Underline;
                this.Children.Add(Lbl_ID);
                ///FName
                TextBlock Lbl_FName = new TextBlock();
                Lbl_FName.Text = "Имя: " + persona.Fname;
                this.Children.Add(Lbl_FName);
                ///LName
                TextBlock Lbl_LName = new TextBlock();
                Lbl_LName.Text = "Фамилия: " + persona.Lname;
                this.Children.Add(Lbl_LName);
                ///Age
                TextBlock Lbl_Age = new TextBlock();
                Lbl_Age.Text = "Возраст: " + persona.Age.ToString();
                this.Children.Add(Lbl_Age);
                ///Edit
                Button Btn_Edit = new Button();
                Btn_Edit.Content = "Редактировать";
                Btn_Edit.ManipulationStarted += new System.EventHandler<ManipulationStartedEventArgs>(OpenEditForm);
                this.Children.Add(Btn_Edit);
                ///Delete
                Button Btn_Del = new Button();
                Btn_Del.Content = "Удалить";
                Btn_Del.ManipulationStarted += new System.EventHandler<ManipulationStartedEventArgs>(OpenDeleteForm);
                this.Children.Add(Btn_Del);
            }
        }

        public void UpdatePersona()
        {
            //((PictureBox)this.Controls[0]).Image = global::PersonsWinForm.Properties.Resources.photo;
            ((System.Windows.Controls.TextBlock)this.Children[1]).Text = "ID: " + persona.ID.ToString();
            ((System.Windows.Controls.TextBlock)this.Children[2]).Text = "Имя: " + persona.Fname;
            ((System.Windows.Controls.TextBlock)this.Children[3]).Text = "Фамилия: " + persona.Lname;
            ((System.Windows.Controls.TextBlock)this.Children[4]).Text = "Возраст: " + persona.Age.ToString(); 
        }

        public void OpenEditForm(object sender, ManipulationStartedEventArgs e)
        {
            (Application.Current as App).persona = persona;
            Microsoft.Phone.Controls.PhoneApplicationPage pg =(Microsoft.Phone.Controls.PhoneApplicationPage)((Grid)((Grid)((ScrollViewer)((StackPanel)Parent).Parent).Parent).Parent).Parent;
            pg.NavigationService.Navigate(new Uri("/PAddPerson.xaml", UriKind.Relative));
            e.Complete();
            e.Handled = true;
        }

        public void OpenDeleteForm(object sender, EventArgs e)
        {
            try
            {
                string msg = "Вы действительно хотите удалить персону с ID = " + persona.ID.ToString() + "?";
                MessageBox.Show(msg, "Удаление", MessageBoxButton.OKCancel);
                (Application.Current as App).list.Delete(persona.ID);
                System.Windows.Controls.StackPanel parent = (StackPanel)Parent;
                ((StackPanel)Parent).Children.Clear();
                foreach (Persona per in (Application.Current as App).list)
                {
                    DrawPerson dp = new DrawPerson(per);
                    parent.Children.Add(dp);
                    dp.Draw();
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK);
            }
        }
    }
}
